<template>
  <div>


    <div class="search-box">
      <div class="search-item">
        <div class="search-label">状态:</div>
        <div class="search-content">
          <el-select v-model="permission" placeholder="请选择"  size="mini" clearable="">
            <el-option  key="0" label="待审核" value="0"></el-option>
            <el-option  key="1" label="通过" value="1"></el-option>
            <el-option  key="-1" label="未通过" value="-1"></el-option>
          </el-select>
        </div>
      </div>

      <div class="search-item">
        <el-button type="primary" size="mini" icon="el-icon-search" @click="loadResources">查询</el-button>
      </div>

    </div>


    <el-main style="padding-left: 0px;padding-top: 0px">




          <el-table
            :data="objs"
            v-loading="tableLoading"
            border
            stripe
            @selection-change="handleSelectionChange"
            size="mini"
            style="width: 100%">
            <el-table-column
              type="selection"
              align="left"
            >
            </el-table-column>
            <el-table-column
              prop="name"
              label="名称"
            >
            </el-table-column>



            <el-table-column
              prop="permission"
              :formatter="toPermissionLabel"
              label="状态"
            >



            </el-table-column>



            <el-table-column
              label="操作">
              <template slot-scope="scope">
                <el-button @click="showEditView(scope.row)" type="text"
                           size="mini">审核
                </el-button>
                <el-button @click="openFileView(scope.row)" type="text"
                           size="mini">查看
                </el-button>
                <el-button type="text" size="mini"
                           @click="deleteObj(scope.row)">删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>

          <el-pagination
            background
            :page-size="10"
            :current-page="currentPage"
            @current-change="currentChange"
            layout="prev, pager, next"
            :total="totalCount">
          </el-pagination>
          <div class="form-edit-btns" style="float:right">
            <el-button type="danger" size="mini" v-if="objs.length>0" :disabled="multipleSelection.length==0"
                       @click="deleteMany">批量删除
            </el-button>
          </div>







    </el-main>

    <el-dialog
      :title="dialogTitle"
      style="padding: 0px;"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="77%">

      <el-form :model="obj" :rules="rules" ref="addForm" size="mini" label-width="100px">


        <el-form-item label="名称:" prop="name">
          <el-input prefix-icon="el-icon-edit" v-model="obj.name" size="mini"
                    placeholder="请输入直播活动名称"></el-input>
        </el-form-item>





        <el-form-item label="课程" prop="course_id">
          <el-select v-model="obj.course_id" placeholder="请选择">
            <el-option
              v-for="item in course_options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>






<!--        <el-form-item label="理由:" prop="reason">-->
<!--          <el-input prefix-icon="el-icon-edit" v-model="obj.reason" size="mini"-->
<!--                    placeholder="请输入理由"></el-input>-->
<!--        </el-form-item>-->





        <el-form-item>
          <el-button size="mini" v-if="obj.permission==0" type="danger" @click="operate('addForm',-1)">拒 绝</el-button>
          <el-button size="mini" v-if="obj.permission==0" type="primary" @click="operate('addForm',1)">通 过</el-button>
        </el-form-item>


      </el-form>

    </el-dialog>

  </div>

</template>
<script>
  import ElRow from "element-ui/packages/row/src/row";

  export default {
    components: {ElRow},
    data() {
      return {
        activeName: 'zhibo',
        objs: [],
        dialogTitle: '',
        multipleSelection: [],
        course_options:[],
        teacher_options:[],
        totalCount: -1,
        currentPage: 1,
        dialogVisible: false,
        tableLoading: false,
        permission:'',
        obj: {
          name: '',
          course_id:'',
          permission:0,
          // reason:''
        },
        rules: {
          name: [{required: true, message: '必填:请输入直播活动名称', trigger: 'blur'}],
          course_id: [{required: true, message: '必填:请选择课程', trigger: 'blur'}],
        }
      };
    },
    mounted: function () {
      this.loadTeacherList();
      this.loadCourses();
      this.loadResources();
    },
    methods: {


      //时间格式化
      dateFormat:function(row, column) {
        var date = row[column.property];
        if (date == undefined) {
          return "";
        }
        return moment(date).utcOffset(480).format("YYYY-MM-DD HH:mm:ss");
      },

      toPermissionLabel:function (row) {
        if (row.permission==0) {
          return "待审核";
        } else if (row.permission==1) {
          return "通过";
        } else if (row.permission==-1) {
          return "未通过";
        }
      },

      loadTeacherList() {
        var _this = this;
        this.tableLoading = true;
        this.postRequest("/user/getTeacherList").then(resp=> {
          this.tableLoading = false;
          if (resp && resp.status == 200) {
            var data = resp.data;
            _this.teacher_options = data.data.arrays;
          }
        })
      },


      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      deleteMany(){
        this.$confirm('此操作将删除[' + this.multipleSelection.length + ']条数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var ids = '';
          for (var i = 0; i < this.multipleSelection.length; i++) {
            ids += this.multipleSelection[i].id + ",";
          }
          this.doDelete(ids);
        }).catch(() => {
        });
      },
      deleteObj(row){
        this.$confirm('此操作将永久删除[' + row.name + '], 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.doDelete(row.id);
        }).catch(() => {
        });
      },
      doDelete(ids){
        this.tableLoading = true;
        var _this = this;
        this.deleteRequest("/resource/delete/" + ids).then(resp=> {
          _this.tableLoading = false;
          if (resp && resp.status == 200) {
            var data = resp.data;
            _this.$message({type: data.status, message: data.msg});
            _this.loadResources();
          }
        })
      },


      currentChange(currentChange){
        this.currentPage = currentChange;
        this.loadResources();
      },


      loadResources(){
        var _this = this;
        this.tableLoading = true;
        this.getRequest("/resource/list?page=" + this.currentPage + "&size=10&permission="+this.permission).then(resp=> {
          this.tableLoading = false;
          if (resp && resp.status == 200) {
            var data = resp.data;
            _this.objs = data.data.arrays;
            _this.totalCount = data.data.count;
          }
        })
      },



      loadCourses(){
        var _this = this;
        this.tableLoading = true;
        this.postRequest("/course/list").then(resp=> {
          this.tableLoading = false;
          if (resp && resp.status == 200) {
            var data = resp.data;
            _this.course_options = data.data.arrays;
          }
        })
      },


      operate(formName,permssion){
        var _this = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.obj.id) {
              //更新
              this.tableLoading = true;
              this.obj.permission = permssion;
              this.postRequest("/resource/update", this.obj).then(resp=> {
                _this.tableLoading = false;
                if (resp && resp.status == 200) {
                  var data = resp.data;
                  _this.$message({type: data.status, message: data.msg});
                  _this.dialogVisible = false;
                  _this.emptyObjData();
                  _this.loadResources();
                }
              })
            }
          } else {
            return false;
          }
        });
      },
      openFileView(row){
        location.href = row.record_url;
      },

      showEditView(row){
        //console.log(row)
        this.dialogTitle = "审核直播活动";
        this.obj.name = row.name;
        this.obj.course_id = row.course_id;
        this.obj.id = row.id;
        this.obj.permission = row.permission;
        // this.obj.reason = row.reason;
        this.dialogVisible = true;

      },
      emptyObjData(){
        this.obj = {
          name: '',
          course_id:'',
          permission:0,
          // reason:''

        }
      }
    }
  };
</script>
<style scoped>
  .el-dialog__body {
    padding-top: 0px;
    padding-bottom: 0px;
  }

  .slide-fade-enter-active {
    transition: all .8s ease;
  }

  .slide-fade-leave-active {
    transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
  }

  .slide-fade-enter, .slide-fade-leave-to {
    transform: translateX(10px);
    opacity: 0;
  }
</style>
